#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework3_6.py
# author:wmf
# datetime:2021/4/6 21:05
# software: PyCharm
'''
this is function description
'''
# import module your need

# 对一篇英文小说，进行词频统计，输出前20个出现频率最高的单词；
#         英文小说"老人与海"     链接：https://pan.baidu.com/s/1goqK3zF8VBUFuD_2ezfZ7Q   提取码：mv04
#     提示：
#     1 可以先定义一个函数，专门来处理英文文章的读取问题；读取时，解决大小写问题，以及标点符号问题（如，将标点符号都替换成空格）；

import os

def getText(file): # 解决标点符号和大小写问题
    txt = file.read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_\'{|}~':
        txt = txt.replace(ch, " ")
    return txt

def getWord(txt): # 将单词与单词分开
    txt = txt.split()
    return txt

def getCount(txt): # 获取每个单词的出现次数
    dict1 = {}
    for x in txt:
        if(x in dict1):
            dict1[x] = dict1[x] +1
        else:
            dict1[x] = 1
    return dict1

def getMaxTwenty(dict1): # 得到前20个出现频率最高的单词
    list1 = sorted(dict1.items(),key = lambda item:item[1],reverse=True)
    for i in range(0,20):
        print(list1[i])

with open('The Old Man And The Sea.txt','r',encoding = 'utf-8') as f:
    getMaxTwenty(getCount(getWord(getText(f))))
